package order;

import model.*;
import javax.swing.JOptionPane;


public class UpgradeTroop extends UpgradeOrder{
  
	private Territory territory;
	private Player player;
	private TechnologyLevel source;
	private TechnologyLevel destination;
	private int amount;
    private int cost;
       
    public UpgradeTroop(Territory territory, Player player, TechnologyLevel source, TechnologyLevel destination, int amount){
    	this.territory = territory;
    	this.player = player;
    	this.source = source;
    	this.destination = destination;
    	this.amount = amount;
    	cost = (destination.TroopCost() - source.TroopCost()) * amount;
    }
    
    public Territory getSource(){return territory;}
    public Territory getDestination(){return territory;}
    public boolean checkOrder(){  	
    	if (player.getTechnology() < cost){
    		JOptionPane.showMessageDialog(null, territory.getLocation() + "No enough technology to upgrade!");
    		return false;
    	}
    	else return true;
    }
    
    public void undoOrder(){
         player.addTechnology(cost);
         territory.upgradeTroop(destination, source, amount, player); 	
   }
    
    public  String printOrder(){
    	StringBuffer sentence = new StringBuffer();
    	sentence.append("At " + territory.getLocation() + ": ");
    	sentence.append("Upgrade ");
    	sentence.append(amount + " ");
    	sentence.append(source + " ");
    	sentence.append("to " + destination + " ");
       	sentence.append(" (Cost " + cost + " Technology) ");
    	return sentence.toString();
    }

	@Override
	public void executeOrder() {
		player.cutTechnology(cost);
        territory.upgradeTroop(source, destination, amount, player); 	
	}
}
